var pnum = "";  
var initData = function(num){
	    var param = {
			page_num:num,
			page_size:"10"
	    }
		$.ajax({
	        url: base + "/dataCenter/businessdata/findPutForWardList",
	        data: param,
	        success: function (data, textStatus, jqXHR) {
	        	if(data != null){
		        	var pageCount = data.pager.pageCount;
		        	var dataObj ={
		        			"list":data.list,
		         			"dataTool":{
		         				"title":{
		          			      	"putford_code": "提出资料编号"
		          			      	,"putford_name": "提出资料名称"
		          			      	,"putford_type":"是否是结构化数据"
		          			      	,"project_design_content_name":"设计内容名称"
		          			      	,"project_design_major_name":"设计专业名称"
		          			      	,"project_design_phase_name":"设计阶段名称"
		          			      	,"create_time":"创建时间"
		          			      	,"update_time":"修改时间"
		          			      	,"operation":"操作"
		            			},
		            			"titleCheckbox":false,
								"btn":[{"event":"updateObj","name":"编辑"},
								       {"event":"editCompoundRule","name":"编辑合成规则"},
								       {"event":"delObj","name":"删除"}]
							}
			  		};
		        	showPageTable('view', dataObj,num,pageCount,"initData");
	        	}
	        },
	        error: function () {
	        }
	    });
}
  
  //编辑合成规则
  var editCompoundRule = function(obj){
		var contentDiv = function(){
			var content;
			var data={
					"title":{
						    "id":"id",
                        	"mention_data":"选择提入资料信息",
                        	"design_cell_data":"选择设计单元信息",
						    "compound_template":"选择模板信息",
						    "compound_rule":"模板合成规则"
					},"text":{
						    "id":obj.id
                    		,"mention_data":""
                    		,"design_cell_data":""
							,"compound_template":""
							,"compound_rule":obj.compound_rule==null?"":obj.compound_rule
					},"input":{
						    "id":"hidden"
                   			 ,"mention_data":"new_checkbox"
                    		,"design_cell_data":"new1_checkbox"
							,"compound_template":"checkbox"
							,"compound_rule":"textarea"
					},"input_init":{
					}
			};
			var getDiv =  dc_update.innerHTML;
			laytpl(getDiv).render(data, function(html){
				content = html;
			}); 
			return content;
		}
		layopenCompound(contentDiv());
        init_mention_data(obj.id,obj.content_id,obj.phase_id);
        init_design_cell_data(obj.id,obj.content_id,obj.phase_id);
      	init_compound_template(obj.id,obj.content_id,obj.phase_id);

  }
  
  var init_compound_template = function(template_id , content_id , phase_id){
	  if(template_id == "" || content_id == "" || phase_id == ""){
		  return false;
	  }
	  $.post(base+"/dataCenter/businessdata/findPutForWardCompoundTemplateByTemplate",{template_id:template_id,content_id:content_id,phase_id:phase_id},
	   		  	function(data,status){
     		    	if(data!=''){
   	      		  	 var checkboxString = "";
   	                 $.each(data, function(i,item){
	                		if(item.is_include != null && item.is_include != ""){
	                			checkboxString += " <input name = "+item.id+" title = "+item.template_name+" type='checkbox' checked='' >";
	                		}else{
	                			checkboxString += " <input name = "+item.id+" title = "+item.template_name+" type='checkbox'>";
	                		}
   	                });
	   	                $("#checkbox").html(checkboxString);
	   	      		}else{
	   	      		  	$("#checkbox").html("");
	   	      		}
	     		    form.render(); 
	  });
}

  //初始化该设计阶段下该设计内容下的提入资料
  var init_mention_data = function(template_id , content_id , phase_id){
      if(template_id == "" || content_id == "" || phase_id == ""){
          return false;
      }
      $.post(base+"/dataCenter/businessdata/findPutForWardMentionDataByTemplate",{template_id:template_id,content_id:content_id,phase_id:phase_id},
          function(data,status){
              if(data!=''){
                  var checkboxString = "";
                  $.each(data, function(i,item){
                      if(item.is_include != null && item.is_include != ""){
                          checkboxString += " <input name = "+item.id+" title = "+item.mention_information_name+" type='checkbox' checked='' >";
                      }else{
                          checkboxString += " <input name = "+item.id+" title = "+item.mention_information_name+" type='checkbox'>";
                      }
                  });
                  $("#new_checkbox").html(checkboxString);
              }else{
                  $("#new_checkbox").html("");
              }
              form.render();
          });

  }

  //初始化该设计阶段下该设计内容下的设计单元信息
  var init_design_cell_data = function(template_id , content_id , phase_id){
      if(template_id == "" || content_id == "" || phase_id == ""){
          return false;
      }
      $.post(base+"/dataCenter/businessdata/findPutForWardDesignCellDataByTemplate",{template_id:template_id,content_id:content_id,phase_id:phase_id},
          function(data,status){
              if(data!=''){
                  var checkboxString = "";
                  $.each(data, function(i,item){
                      if(item.is_include != null && item.is_include != ""){
                          checkboxString += " <input name = "+item.id+" title = "+item.design_cell_table_name+" type='checkbox' checked='' >";
                      }else{
                          checkboxString += " <input name = "+item.id+" title = "+item.design_cell_table_name+" type='checkbox'>";
                      }
                  });
                  $("#new1_checkbox").html(checkboxString);
              }else{
                  $("#new1_checkbox").html("");
              }
              form.render();
          });
  }
  //渲染弹窗
  var layopenCompound = function(content){
      layer.open({
          title:'配置模板合成规则'
          ,content: content
          ,area: ['800px', '600px']
      	   ,scrollbar: false
          ,btn: ['确认', '取消']
          ,yes: function(index,layero){ //确认
              var data = $(layero).find("form").serializeJSON();
              if(data.compound_rule == ""){
      			 var obj = $("textarea[name='compound_rule']");
   			     obj.focus();
   			     layer.tips('请填写此字段!', obj);
   			     return false;
              }
              var mention_data = new Array();
              var design_cell_data = new Array();
              $.each($("#new_checkbox").children("input[type=checkbox]:checked"),function(){
                  mention_data.push($(this).attr("name"));
              });
              $.each($("#new1_checkbox").children("input[type=checkbox]:checked"),function(){
                  design_cell_data.push($(this).attr("name"));
              });

              var compound_template = new Array();
			   $.each($("#checkbox").children("input[type=checkbox]:checked"),function(){
				   compound_template.push($(this).attr("name"));
			   });
				$.post(base+"/dataCenter/businessdata/updatePutForWardCompoundRule",{
					id:data.id==null?"":data.id,
                    mention_data:$.trim(mention_data.join(",")),
					design_cell_data:$.trim(design_cell_data.join(",")),
					compound_template:$.trim(compound_template.join(",")),
					compound_rule:$.trim(data.compound_rule)
				},
		  		function (data,status){
					if(data!=""&&data!=null){
						if(data.statusCode == 0){
							layer.msg('更新成功', {icon: 1,time:2000});
						}else{
							layer.msg(data.message, {icon: 2,time:2000});
						}
					}else{
						layer.msg('更新失败!', {icon: 2,time:2000});
					}
					initData(pnum);
		  		 })
          }
      });
  }
  
  
  
  var searchObj = function (num) {
      var name=$.trim($("#searchName").val());
      var param = {
          page_num:num,
          page_size:"10",
          putford_name:name
      }
		$.ajax({
	        url: base + "/dataCenter/businessdata/findPutForWardList",
	        data: param,
	        success: function (data, textStatus, jqXHR) {
	        	if(data != null){
		        	var pageCount = data.pager.pageCount;
		        	var dataObj ={
		        			"list":data.list,
		         			"dataTool":{
		         				"title":{
		          			      	"putford_code": "提出资料编号"
		          			      	,"putford_name": "提出资料名称"
		          			      	,"putford_type":"是否是结构化数据"
		          			      	,"project_design_content_name":"设计内容名称"
		          			      	,"project_design_major_name":"设计专业名称"
		          			      	,"project_design_phase_name":"设计阶段名称"
		          			      	,"create_time":"创建时间"
		          			      	,"update_time":"修改时间"
		          			      	,"operation":"操作"
		            			},
		            			"titleCheckbox":false,
								"btn":[{"event":"updateObj","name":"编辑"},
								       {"event":"delObj","name":"删除"}]
							}
			  		};
		        	showPageTable('view', dataObj,num,pageCount,"searchObj");
	        	}
	        },
	        error: function () {
	        }
	    });
  }
  
//添加提出资料
var addData = function(){
	   var contentDIV = function(){
		   var content;
		   var data = {
				   "title":{
					   "phase_id":"所属设计阶段",
					   "major_id":"所属设计专业",
					   "content_id":"所属设计内容",
					   "putford_code":"提出资料编号",
					   "putford_name":"提出资料名称",
					   "putford_type":"是否是结构化数据"
				   },"text":{
					   "phase_id":"",
					   "major_id":"",
					   "content_id":"",
					   "putford_code":"",
					   "putford_name":"",
					   "putford_type":""
				   },"input":{
					   "phase_id":"select",
					   "major_id":"new_select",
					   "content_id":"select",
					   "putford_code":"text",
					   "putford_name":"text",
					   "putford_type":"switch"
				   },"input_init":{
				   }
		   };
		   var getDiv = dc_update.innerHTML;
		   laytpl(getDiv).render(data,function(html){
			   content = html;
		   });
		   return content;
	   }
	   layopen(contentDIV());
	   initPhase();
	   initDesignMajor();
	   initSwitch();
	   chanageMajorselect();
}

//初始化提出资料类型
var initSwitch = function(){
	   var html = '<input name="putford_type" lay-skin="switch" lay-filter="switch" lay-text="是|否" type="checkbox" > ';
	   $("#switch").html(html);
	   form.render(); 
}


//初始化专业信息
var initDesignMajor = function(){
	   $.post(base+"/dataCenter/businessdata/findDesignMajorData",{},
 		  	function(data,status){
   		    	if(data!=''){
 	      		  	 var optionstring = "";
 	                 $.each(data.list, function(i,item){
 	                    optionstring += "<option value=\"" + item.id + "\" >" + item.project_design_major_name + "</option>";
 	                });
 	                $("select[name='major_id']").html('<option value=""></option>' + optionstring);
 	      		}else{
 	      		  	$("select[name='major_id']").html('<option value=""></option>');
 	      		}
   		    form.render(); 
});
}

//初始化设计阶段
var initPhase = function(){
	   $.post(base+"/dataCenter/businessdata/findDesignPhaseData",{},
	   		  	function(data,status){
  		    	if(data != null && data.list != null){
	      		  	 var optionstring = "";
	                 $.each(data.list, function(i,item){
	                	optionstring += "<option value=\"" + item.id + "\" >" + item.project_design_phase_name + "</option>";
	                });
	                $("select[name='phase_id']").html('<option value=""></option>' + optionstring);
	      		}else{
	      		  	$("select[name='phase_id']").html('<option value=""></option>');
	      		}
  		    form.render(); 
	  });
 }


//监听专业下拉选
var chanageMajorselect = function(){
		form.on("select(new_select)",function(value){
			var major_id = value.value;
			initContent(major_id);
		});
}

var initContent = function(major_id){
	   $.post(base+"/dataCenter/businessdata/findDesignContent",{major_id:major_id},
	   		  	function(data,status){
	     		    	if(data!=''){
	   	      		  	 var optionstring = "";
	   	                 $.each(data.list, function(i,item){
	   	                    optionstring += "<option value=\"" + item.id + "\" >" + item.project_design_content_name + "</option>";
	   	                });
	   	                $("select[name='content_id']").html('<option value=""></option>' + optionstring);
	   	      		}else{
	   	      		  	$("select[name='content_id']").html('<option value=""></option>');
	   	      		}
	     		    form.render(); 
	  });
}
   
   
//编辑提出资料
var updateObj = function(obj){
	   var contentDIV = function(){
		   var content;
		   var data = {
				   "title":{
					   "id":"主键",
					   "project_design_content_name":"所属设计内容",
					   "project_design_major_name":"所属设计专业",
					   "project_design_phase_name":"所属设计阶段",
					   "putford_code":"提出资料编号",
					   "putford_name":"提出资料名称"
				   },"text":{
					   "id":obj.id,
					   "project_design_content_name":obj.project_design_content_name,
					   "project_design_major_name":obj.project_design_major_name,
					   "project_design_phase_name":obj.project_design_phase_name,
					   "putford_code":obj.putford_code,
					   "putford_name":obj.putford_name
				   },"input":{
					   "id":"hidden",
					   "project_design_content_name":"text_readonly",
					   "project_design_major_name":"text_readonly",
					   "project_design_phase_name":"text_readonly",
					   "putford_code":"text",
					   "putford_name":"text"
				   },"input_init":{
				   }
		   };
		   var getDiv = dc_update.innerHTML;
		   laytpl(getDiv).render(data,function(html){
			   content = html;
		   });
		   return content;
	   }
	   layopen(contentDIV());
}

//打开弹窗
var layopen = function(content){
	layer.open({
	  	title:'更新提出资料信息'
		,content: content
    	,area: ['800px', '600px']
	    ,scrollbar: false
	  	,btn: ['确认', '取消']
		,yes: function(index,layero){ //确认
		  	var data = $(layero).find("form").serializeJSON();
		  	if(verification_data(data)){
				$.post(base+"/dataCenter/businessdata/updatePutForWard",data,
				  		function (data,status){
							if(data!=""&&data!=null){
								if(data.statusCode == 0){
									layer.msg('更新成功', {icon: 1,time:2000});
								}else{
									layer.msg(data.message, {icon: 2,time:2000});
								}
							}else{
								layer.msg('更新失败!', {icon: 2,time:2000});
							}
							initData(pnum);
				  		 })
		  	}
		}
});
}

//验证数据
var verification_data = function(data){
	if(data.putford_code == ""){
		var obj = $("input[name='putford_code']");
		obj.focus();
		layer.tips('请填写提出资料代号', obj);
		return false;
	}
	if(data.putford_name == ""){
		var obj = $("input[name='putford_name']");
		obj.focus();
		layer.tips('请填写提出资料名称', obj);
		return false;
	}
	if(data.putford_type == "on"){
		data.putford_type = true;
	}else{
		data.putford_type = false;
	}
	if(data.major_id == ""){
		alert("请选择设计专业!");
		return false;
	}
	if(data.content_id == ""){
		alert("请选择设计内容!")
		return false;
	}
	return true;
}


//删除提出资料
var delObj = function(obj){
	   layer.confirm('确认删除?',{btn : [ '确定', '取消' ]},function(index){
		   layer.close(index);
		   var param = {id:obj.id}
		   $.ajax({
			   url:base+"/dataCenter/businessdata/delPutForWardById",
			   data:param,
			   success:function(data,status){
				    if(data.statusCode == 0){
						layer.msg(data.message, {
							icon : 1
						});
				    }else{
						layer.msg(data.message, {
							icon : 5
						});
				    }
				    initData(pnum);
			   }
		   });
	   });
}

$(document).ready(function(){
	 initData(1);
	 $("#addData").click(addData);
});